package com.hopeland.androidpc.jilintongdaomen.uhf;

import java.util.HashMap;

import com.hopeland.androidpc.jilintongdaomen.PublicData;
import com.rfidread.Interface.IAsynchronousMessage;
import com.rfidread.RFIDReader;
import com.util.BaseActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.util.Log;


/**
 * @author RFID_lx Activity 基类
 */
public class UHFBaseActivity extends BaseActivity {

	protected static Boolean _UHFSTATE = false; // 模块是否已经打开
	// static int _PingPong_ReadTime = 10000; // 默认是100:3
	// static int _PingPong_StopTime = 300;
	protected static int _NowAntennaNo = 1; // 读写器天线编号
	protected static int _NowAntennaNoPower = 1; // 读写器天线编号
	protected static int _UpDataTime = 0; // 重复标签上传时间，控制标签上传速度不要太快
	protected static int _Max_Power = 30; // 读写器最大发射功率
	protected static int _Min_Power = 0; // 读写器最小发射功率
	protected static int _ReaderAntennaCount = 1;//读写器天线个数


	//当前连接的ID
	public static String ConnID = "";
	//当前连接索引
	public static int connectIndex = 0;
	//当前选择读标签的天线号，例如“1&2&4”，当前选择1、2、4天线读标签




	/**
	 * 模块初始化(串口)
	 *
	 * @param log
	 *            接口回调方法
	 * @return 是否初始化成功
	 */
	public Boolean Rfid_RS232_Init(String connParam, IAsynchronousMessage log) {
		Boolean rt = false;
		try {
			if (_UHFSTATE == false) {
				rt = RFIDReader.CreateSerialConn(connParam, log);
				if (rt) {
					ConnID = PublicData.serialParam = connParam;
					connectIndex=0;
					_UHFSTATE = true;
				}
				Thread.sleep(500);
			} else {
				rt = true;
			}
		} catch (Exception ex) {
			Log.d("debug", "On the UHF electric abnormal:" + ex.getMessage());
		}
		return rt;
	}




	/**
	 * 模块释放
	 */
	public void UHF_Dispose() {
		if (_UHFSTATE == true) {
			RFIDReader.CloseConn(ConnID);
			_UHFSTATE = false;
		}
	}

	/**
	 * 获得读写器的读写能力
	 */
	@SuppressLint("UseSparseArrays")
	@SuppressWarnings("serial")
	protected void UHF_GetReaderProperty() {
		String propertyStr = RFIDReader.GetReaderProperty(ConnID);
		//Log.d("Debug", "Get Reader Property:" + propertyStr);
		String[] propertyArr = propertyStr.split("\\|");
		if (propertyArr.length > 3) {
			try {
				_Min_Power = Integer.parseInt(propertyArr[0]);
				_Max_Power = Integer.parseInt(propertyArr[1]);
				_ReaderAntennaCount = Integer.parseInt(propertyArr[2]);
				//_NowAntennaNo = hm_Ant_value.get(_ReaderAntennaCount);
			} catch (Exception ex) {
				Log.d("Debug", "Get Reader Property failure and conversion failed!");
			}
		} else {
			Log.d("Debug", "Get Reader Property failure");
		}
	}

	/**
	 * 设置标签上传参数
	 */
	protected void UHF_SetTagUpdateParam() {
		// 先查询当前的设置是否一致，如果不一致才设置
		String	searchRT  = "";
		try {
			searchRT = RFIDReader._Config.GetTagUpdateParam(ConnID);
			String[] arrRT = searchRT.split("\\|");
			if (arrRT.length >= 2) {
				int nowUpDataTime = Integer.parseInt(arrRT[0]);
				int rssiFilter = Integer.parseInt(arrRT[1]);
				Log.d("Debug", "Check the label to upload time:" + nowUpDataTime);
				if (_UpDataTime != nowUpDataTime) {
					String param="1,"+_UpDataTime;
					RFIDReader._Config.SetTagUpdateParam(ConnID,_UpDataTime, rssiFilter); // 设置标签重复上传时间为20ms
					Log.d("Debug", "Sets the label upload time...");
				} else {
				}
			} else {
				Log.d("Debug", "Query tags while uploading failure...");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
